package com.example.demo.多线程.线程通信;

import org.springframework.web.bind.annotation.ControllerAdvice;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

@ControllerAdvice
public class Book2 {
    private int number = 10;

    // 还书
    public synchronized void increment() throws InterruptedException {
        while (number == 10) {
            this.wait();
        }
        System.out.println(Thread.currentThread().getName() + ",还了第" + number++ + "书");
        this.notifyAll();
    }

    // 借书
    public synchronized void decrement() throws InterruptedException {
        while (number == 0) {
            this.wait();
        }
        System.out.println(Thread.currentThread().getName() + ",借出第" + number-- + "书");
        this.notifyAll();
    }
}
